package com.lanou.bookstore.user.web.servlet;

import com.lanou.bookstore.book.domain.Book;
import com.lanou.bookstore.cart.domain.Cart;
import com.lanou.bookstore.cart.domain.CartItem;
import com.lanou.bookstore.user.domain.User;
import com.lanou.bookstore.user.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/user")
public class UserServlet extends HttpServlet {
    UserService userService = new UserService();

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String method = request.getParameter("method");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String email = request.getParameter("email");
        if ("regist".equals(method)) {
            User form = new User(null, username, password, email, null, null);
            boolean result = userService.regist(form);
            if (result) {
                request.setAttribute("msg", "用户注册成功");
                request.getRequestDispatcher("/jsps/msg.jsp").forward(request, response);
            } else {
                request.setAttribute("msg", "用户注册失败");
                request.getRequestDispatcher("/jsps/msg.jsp").forward(request, response);
            }
        } else {
            User form = new User(null, username, password, null, null, null);
            boolean result = userService.login(form);
            if (result) {
                if ("admin".equals(form.getUsername())) {
                    request.getSession().setAttribute("user", form);
                    response.sendRedirect("/adminjsps/admin/main.jsp");
                } else {
                    Book book = new Book();
                    CartItem cartItem = new CartItem(book, null);
                    Map<String, CartItem> map = new HashMap<String, CartItem>();
                    map.put(null, cartItem);
                    Cart carts = new Cart();
                    carts.setCartItems(map);
                    User user = userService.findAllByUsername(username);
                    request.getSession().setAttribute("user", user);
                    request.getSession().setAttribute("carts", carts);
                    response.sendRedirect("/jsps/main.jsp");
                }
            } else {
                request.setAttribute("msg", "用户名或密码错误！");
                request.getRequestDispatcher("/jsps/user/login.jsp").forward(request, response);
            }
        }

    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        session.removeAttribute("user");
        request.getRequestDispatcher("/jsps/user/login.jsp").forward(request, response);
    }
}
